#include <QUdpSocket>
#include <QTimer>
#include "agenttaskeventtrigger.h"
#include "macro.h"
#include "Agent/agentprotocol.h"

#define EVENT_HEART_INTERVAL_MILLISECS 60000  //心跳间隔

AgentTaskEventTrigger::AgentTaskEventTrigger(QMap <QByteArray,QByteArray> mapIPPortReqPacket,QByteArray packet,
     QString paramAppIP, quint16 paramAppPort, QObject *parent)
    :AgentTaskBase(mapIPPortReqPacket,packet,paramAppIP,paramAppPort,parent)
{

}
void AgentTaskEventTrigger::slotInitDerivedClass()
{

    isDuringHold=false;
    timerForHold=new QTimer(this);
    timerForEventHeart=new QTimer(this);
    timerForEventHeart->start(EVENT_HEART_INTERVAL_MILLISECS);
    connect(timerForEventHeart,SIGNAL(timeout()),this,SLOT(slotTimerEventHeart()));
    connect(timerForHold,SIGNAL(timeout()),this,SLOT(slotHold()));
    QByteArray packetToBeSent="Event Watch "+QByteArray::number(fieldSrcMac);
    slotSendPacket(packetToBeSent,aServerIP,aServerPort);

    isCreatedSuccessfully=true;

}
void AgentTaskEventTrigger::analyzeIncomeDatagram(QByteArray packet)
{
    QByteArray packetUpper=packet.simplified().toUpper();

    if(packetUpper.startsWith("ERR"))
    {
        emit sigSendDataToApp(agentProtocol->replyLineBadRequest()+"\n\n"+
            packet,appIP,appPort);
        deleteLater();
        return;
    }
    else if(packetUpper.startsWith("INFO")||packetUpper.startsWith("WARN"))
    {
        emit sigSendDataToApp(packet,appIP,appPort);
        return;
    }
    else if(packetUpper.startsWith("EVENT"))
    {
        QList <QByteArray> listFieldsInEvent=packetUpper.split(' ');
        bool ok;

        if(listFieldsInEvent.size()!=5)
            return;
        else if(listFieldsInEvent.at(1).toLongLong(&ok)!=fieldSrcMac)
            return;
        else if(listFieldsInEvent.at(2).toUpper()!="SENSORDATA")
            return;
        else if(listFieldsInEvent.at(3).toUpper()!=fieldSrcSensorName.toUpper())
            return;
        else if(isComparisonHit(listFieldsInEvent.at(4)))
        {
            //emit sigSendDataToApp("满足触发条件",appIP,appPort);

            if(isDuringHold) //如果正在hold期间，则不作任何处理
                return;
            else //当前未在hold期间，则进入hold状态
            {
                //emit sigSendDataToApp("进入hold状态",appIP,appPort);

                isDuringHold=true;
                timerForHold->start(fieldHoldMilliSeonds);
            }
        }
        else
        {
            //emit sigSendDataToApp("未满足触发条件",appIP,appPort);

            timerForHold->stop();
            isDuringHold=false;
        }

    }
}

void AgentTaskEventTrigger::slotHold()
{
    if(isDuringHold)
    {
        emit sigSetIPAnalyzeIncomeDatagram(fieldPayLoad,QHostAddress(appIP),appPort);
        timerForHold->stop();
        isDuringHold=false;
    }
    timerForHold->stop();
    isDuringHold=false;

}

void AgentTaskEventTrigger::slotTimerEventHeart()
{
    QByteArray packetToBeSent="Event Alive "+QByteArray::number(fieldSrcMac);
    slotSendPacket(packetToBeSent,aServerIP,aServerPort);    
}

AgentTaskEventTrigger::~AgentTaskEventTrigger()
{

}
